package com.ljy.farm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ljy.farm.datasource.entities.Organization;
import com.ljy.farm.datasource.vo.TreeNode;
import com.ljy.farm.utils.BaseResponseInfo;
import com.ljy.farm.constants.ExceptionConstants;
import com.ljy.farm.exception.BusinessRunTimeException;
import com.ljy.farm.service.organization.OrganizationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.List;


@RestController
@RequestMapping(value = "/organization")
public class OrganizationController {
    private Logger logger = LoggerFactory.getLogger(OrganizationController.class);

    @Resource
    private OrganizationService organizationService;

    /**
     * 根据id来查询机构信息
     *
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/findById")
    public BaseResponseInfo findById(@RequestParam(value = "id", required = false) Long id, HttpServletRequest request) throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        if (id == null) {
            res.code = 400;
            res.data = "参数id不能为空";
            return res;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            List<Organization> dataList = organizationService.findById(id);
            JSONObject outer = new JSONObject();
            if (null != dataList) {
                for (Organization org : dataList) {
                    outer.put("id", org.getId());
                    outer.put("orgFullName", org.getOrgFullName());
                    outer.put("orgAbr", org.getOrgAbr());
                    outer.put("orgParentNo", org.getOrgParentNo());
                    List<Organization> dataParentList = organizationService.findByOrgNo(org.getOrgParentNo());
                    if (dataParentList != null && dataParentList.size() > 0) {
                        //父级机构名称显示简称
                        outer.put("orgParentName", dataParentList.get(0).getOrgAbr());
                    }
                    outer.put("orgTpcd", org.getOrgTpcd());
                    outer.put("orgStcd", org.getOrgStcd());
                    outer.put("orgNo", org.getOrgNo());
                    outer.put("sort", org.getSort());
                    /**
                     * 判断时间不为空再做转化
                     * */
                    if (org.getOrgCreateTime() != null) {
                        outer.put("orgCreateTime", sdf.format(org.getOrgCreateTime()));
                    }
                    if (org.getOrgStopTime() != null) {
                        outer.put("orgStopTime", sdf.format(org.getOrgStopTime()));
                    }
                    outer.put("remark", org.getRemark());
                }
            }
            res.code = 200;
            res.data = outer;
        } catch (Exception e) {
            e.printStackTrace();
            res.code = 500;
            res.data = "获取数据失败";
        }
        return res;
    }

    /**
     * create by: cjl
     * description:
     * 获取机构树数据
     * create time: 2019/2/19 11:49
     *
     * @return com.alibaba.fastjson.JSONArray
     * @Param:
     */
    @RequestMapping(value = "/getOrganizationTree")
    public JSONArray getOrganizationTree(@RequestParam(value = "id", required = false) Long id) throws Exception {
        JSONArray arr = new JSONArray();
        if (id == null) {
            id = -1L;
        }
        List<TreeNode> organizationTree = organizationService.getOrganizationTree(id);
        if (organizationTree != null && organizationTree.size() > 0) {
            for (TreeNode node : organizationTree) {
                String str = JSON.toJSONString(node);
                JSONObject obj = JSON.parseObject(str);
                arr.add(obj);
            }
        }
        return arr;
    }

    /**
     * create by: cjl
     * description:
     * 新增机构信息
     * create time: 2019/2/19 17:17
     *
     * @return java.lang.Object
     * @Param: beanJson
     */
    @RequestMapping(value = "/addOrganization")
    public Object addOrganization(@RequestParam("info") String beanJson) throws Exception {
        JSONObject result = ExceptionConstants.standardSuccess();
        Organization org = JSON.parseObject(beanJson, Organization.class);
        int i = organizationService.addOrganization(org);
        if (i < 1) {
            throw new BusinessRunTimeException(ExceptionConstants.ORGANIZATION_ADD_FAILED_CODE,
                    ExceptionConstants.ORGANIZATION_ADD_FAILED_MSG);
        }
        return result;
    }

    /**
     * create by: cjl
     * description:
     * 修改机构信息
     * create time: 2019/2/20 9:30
     *
     * @return java.lang.Object
     * @Param: beanJson
     */
    @RequestMapping(value = "/editOrganization")
    public Object editOrganization(@RequestParam("info") String beanJson) throws Exception {
        JSONObject result = ExceptionConstants.standardSuccess();
        Organization org = JSON.parseObject(beanJson, Organization.class);
        int i = organizationService.editOrganization(org);
        if (i < 1) {
            throw new BusinessRunTimeException(ExceptionConstants.ORGANIZATION_EDIT_FAILED_CODE,
                    ExceptionConstants.ORGANIZATION_EDIT_FAILED_MSG);
        }
        return result;
    }

    /**
     * create by: cjl
     * description:
     * 批量删除机构信息
     * create time: 2019/2/19 17:26
     *
     * @return java.lang.Object
     * @Param: ids
     */
    @RequestMapping(value = "/batchDeleteOrganization")
    public Object batchDeleteOrganization(@RequestParam("ids") String ids) throws Exception {
        JSONObject result = ExceptionConstants.standardSuccess();
        int i = organizationService.batchDeleteOrganizationByIds(ids);
        if (i < 1) {
            throw new BusinessRunTimeException(ExceptionConstants.ORGANIZATION_DELETE_FAILED_CODE,
                    ExceptionConstants.ORGANIZATION_DELETE_FAILED_MSG);
        }
        return result;
    }
}
